package com.example.maoyanmovie.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.example.maoyanmovie.R;
import com.example.maoyanmovie.model.Seat;
import java.util.ArrayList;
import java.util.List;

public class SeatAdapter extends RecyclerView.Adapter<SeatAdapter.ViewHolder> {

    private Context context;
    private List<Seat> seatList = new ArrayList<>();
    private OnSeatClickListener listener;
    private int columns;

    public interface OnSeatClickListener {
        void onSeatClick(Seat seat, int position);
    }

    public SeatAdapter(Context context, int columns) {
        this.context = context;
        this.columns = columns;
    }

    public void setSeatList(List<Seat> seatList) {
        this.seatList = seatList;
        notifyDataSetChanged();
    }

    public void setOnSeatClickListener(OnSeatClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_seat, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Seat seat = seatList.get(position);

        // 设置座位号显示
        int row = position / columns;
        int col = position % columns;

        // 每行第一个显示行号
        if (col == 0) {
            holder.tvSeat.setText(String.valueOf(row + 1));
            holder.tvSeat.setTextSize(10);
            holder.tvSeat.setBackgroundColor(Color.TRANSPARENT);
            holder.tvSeat.setTextColor(ContextCompat.getColor(context, R.color.text_hint));
            holder.itemView.setClickable(false);
        } else {
            holder.tvSeat.setTextSize(12);

            // 根据座位状态设置样式
            switch (seat.getStatus()) {
                case Seat.STATUS_AVAILABLE:
                    holder.tvSeat.setText("");
                    holder.tvSeat.setBackgroundResource(R.drawable.seat_available);
                    holder.itemView.setClickable(true);
                    break;
                case Seat.STATUS_SELECTED:
                    holder.tvSeat.setText("");
                    holder.tvSeat.setBackgroundResource(R.drawable.seat_selected);
                    holder.itemView.setClickable(true);
                    break;
                case Seat.STATUS_SOLD:
                    holder.tvSeat.setText("");
                    holder.tvSeat.setBackgroundResource(R.drawable.seat_sold);
                    holder.itemView.setClickable(false);
                    break;
            }

            // 设置点击事件
            holder.itemView.setOnClickListener(v -> {
                if (listener != null && seat.getStatus() != Seat.STATUS_SOLD) {
                    listener.onSeatClick(seat, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return seatList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvSeat;

        ViewHolder(View itemView) {
            super(itemView);
            tvSeat = itemView.findViewById(R.id.tvSeat);
        }
    }
}
